package com.seednia.foundation.framework.service;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.Ordered;

@Aspect
public class ServiceLoggingAspect implements Ordered {
	public static final Log logger = LogFactory.getLog(ServiceLoggingAspect.class);
	
	protected String resourceNameBeginsAfter = ".component.";
	
	public static final int LOG_POINT_EXCEPTION = 1;
	public static final int LOG_POINT_BEFORE = 2;
	public static final int LOG_POINT_AFTER = 4;
	
	public int getOrder()
	{
		return 2;
	}
	
	@Around("execution(* com.ecologixsystem.component.core.command.CommandService.execute(com.ecologixsystem.framework.server.ALT_RequestContext, ..))")
	public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable
	{
		RequestContext reqCtx = (RequestContext) joinPoint.getArgs()[0];
		logger.debug("at log prior invoking "+joinPoint.getTarget().getClass().getName());
		
		/*
		if ( (logPoints & LOG_POINT_BEFORE) != 0 ) {
			
			String arg1 = "";
			if (joinPoint.getArgs().length > 1)
				arg1 = joinPoint.getArgs()[1].toString();
			log(reqCtx, joinPoint, "trace", "Pre-call", "Invoking "+joinPoint.getSignature().getName()+"("+arg1+")", Logger.);
		}
		org.apache.log4j.Level.FATAL.toInt();
		try {
			long startTime = System.currentTimeMillis();
			Object result = joinPoint.proceed();
			if ( (logPoints & LOG_POINT_AFTER) != 0 ) {
				long elapsedTime = System.currentTimeMillis() - startTime;
				log(reqCtx, joinPoint, "trace", "Post-call", "Returned from: "+joinPoint.getSignature().getName()+" (elapsed "+elapsedTime+"ms)", ALT_LoggingInfo.LEVEL_DEBUG);
			}
			return result;

		} catch (Throwable e) {
			if ( (logPoints & LOG_POINT_EXCEPTION) != 0 )
				log(reqCtx, joinPoint, "exception", "Service Exception", "Exception: "+e.getLocalizedMessage(), ALT_LoggingInfo.LEVEL_WARN);
			throw e;
		}*/
		return joinPoint.proceed();
		
	}
	
	protected final void log(RequestContext reqCtx, ProceedingJoinPoint joinPoint
			, String kind, String name, String detail, int logLevel)
	{
		if (joinPoint.getTarget() instanceof IService) {
			IService svc = (IService)joinPoint.getTarget();
			
			logger.info(svc.getId());
		}
		
		
		/*
		if (joinPoint.getTarget().getClass() != getLoggingService().getClass() )
		{
			if (getLoggingService() != null){
				if (joinPoint.getTarget() instanceof ALT_EcoLogixService) {
					ALT_EcoLogixService svc = (ALT_EcoLogixService)joinPoint.getTarget();
					
					ALT_LoggingInfo logInfo = new ALT_LoggingInfo();
					ALT_EcoLogixUtil.initModelToInsert(reqCtx, logInfo);
					logInfo.setDomainSr(reqCtx.getCurrentDomain().getSerial());
					logInfo.setContext(svc.getName());
					logInfo.setKind(kind);
					logInfo.setName(name);
					logInfo.setDetail(detail);
					logInfo.setLogLevel(logLevel);
					getLoggingService().insertRecord(reqCtx, logInfo);
				}
			}
		}
		*/
		
	}
	
	
	public int getLogPoints() {
		return logPoints;
	}
	public void setLogPoints(int logPoints) {
		this.logPoints = logPoints;
	}

	private int logPoints = 4; // 1=at exception, 2=before invocation, 4=after invocation 

	//private LoggingService loggingSvc = null;
}
